package com.javaspeak.concurrency.cas.atomicinteger;

import java.util.concurrent.CountDownLatch;

import com.javaspeak.concurrency.cas.atomicinteger.AtomicIntegerCounter;

/**
 * @author John Dickerson
 */
public class AtomicIntegerCounterTestThread extends Thread {

    private int maxNumber;
    private int[] numbers;

    private CountDownLatch countDownLatch;
    private AtomicIntegerCounter atomicIntegerCounter;


    public AtomicIntegerCounterTestThread( CountDownLatch countDownLatch,
            AtomicIntegerCounter atomicIntegerCounter, int maxNumber ){

        this.countDownLatch = countDownLatch;
        this.atomicIntegerCounter = atomicIntegerCounter;
        this.maxNumber = maxNumber;
        this.numbers = new int[ maxNumber ];

        for ( int i=0; i<numbers.length; i++ ){

            numbers[ i ] = -1;
        }
    }

    public int[] getNumbers(){

        return numbers;
    }


    @Override
    public void run(){

        int currentNumber;

        while ( true ){

            currentNumber = atomicIntegerCounter.incrementByOne();

            if ( currentNumber >= maxNumber ){

                break;
            }
            else {

                numbers[ currentNumber ] = currentNumber;
            }
        }

        countDownLatch.countDown();
    }
}
